/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc;

import midpmpc.ui.MenuOption;
import midpmpc.ui.Progressbar;
import org.j4me.ui.Dialog;
import org.j4me.ui.components.Label;

/**
 *
 * @author vvs
 */
public class NowPlayingView extends Dialog{
    MenuOption state,title,artist,album;
    String currId;
    Progressbar position;
    Progressbar volume;
    Mpc mpc;
    public NowPlayingView(Mpc parent)
    {
        super();
        setTitle("Now playing");
        state=createAndAppend("state");
        title=createAndAppend("title");
        artist=createAndAppend("artist");
        album=createAndAppend("album");
        position=new Progressbar("position");
        volume=new Progressbar("volume");
        append(position);
        append(volume);
        mpc=parent;
        setMenuText(null, "back");
    }
    MenuOption createAndAppend(String label)
    {
        append(new Label(label));
        MenuOption ret=new MenuOption("");
        append(ret);
        return ret;
    }
    public void update()
    {
        state.setString(Mpc.mpdInfo.get("state").toString());
        artist.setString(Mpc.mpdInfo.get("Artist"));
        album.setString(Mpc.mpdInfo.get("Album"));
        title.setString(Mpc.mpdInfo.get("Title"));
        int p,l,t;
        Object to=Mpc.mpdInfo.get("time");
        if (to!=null)
        {
            String pos=to.toString();
            t=pos.indexOf(":");
            p=Integer.valueOf(pos.substring(0,t)).intValue();
            l=Integer.valueOf(pos.substring(t+1)).intValue();
            position.setVal(p, l);
        }
        volume.setVal(Integer.valueOf(Mpc.mpdInfo.get("volume").toString()).intValue(), 100);
//        state.setString(Mpc.mpdInfo.get("state").toString());
//        state.setString(Mpc.mpdInfo.get("state").toString());
    }
    public void keyPressed(int key)
    {
        if (get(getSelected())==position)
        {
            if(key==RIGHT)
            {
                int np=position.getVal()[0]+10;
                MpdConnection.sendCmd("seekid "+Mpc.mpdInfo.get("Id")+" "+String.valueOf(np));
                Mpc.updateStatus();
                update();
                return;
            }
            if (key==LEFT)
            {
                int np=position.getVal()[0]+10;
                MpdConnection.sendCmd("seekid "+Mpc.mpdInfo.get("Id")+" "+String.valueOf(np));
                Mpc.updateStatus();
                update();
                return;
            }

        }
        if (get(getSelected())==volume)
        {
            if(key==RIGHT)
            {
                MpdConnection.sendCmd("volume +5");
                Mpc.updateStatus();
                update();
                return;
            }
            if (key==LEFT)
            {
                MpdConnection.sendCmd("volume -5");
                Mpc.updateStatus();
                update();
                return;
            }

        }
        if (key==MENU_RIGHT)
        {
            mpc.main.show();
            return;
        }
        super.keyPressed(key);
    }
}
